package com.mzy.leetcode;

import com.mzy.ListNode;
import com.mzy.QueueLinked;

public class MergeTwoLists {

    public static class Solution {
        public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
            if (head1 == null || head2 == null) {
                return head1 == null ? head2 : head1;
            }
            ListNode head = head1.val <= head2.val ? head1 : head2;
            ListNode cur1 = head.next;
            ListNode cur2 = head == head1 ? head2 : head1;
            ListNode pre = head;


            while (cur1 != null && cur2 != null) {
                if (cur1.val <= cur2.val) {
                    pre.next = cur1;
                    cur1 = cur1.next;

                } else {
                    pre.next = cur2;
                    cur2 = cur2.next;
                }
                pre = pre.next;
            }
            pre.next = cur1 == null ? cur2 : cur1;

            return head;
        }

        public ListNode mergeTwoList(ListNode head1, ListNode head2) {
            if (head1 == null || head2 == null) {
                return head1 == null ? head2 : head1;
            }
            ListNode head = head1.val < head2.val ? head1 : head2;
            ListNode cur1 = head.next;
            ListNode cur2 = head == head1 ? head2 : head1;
            ListNode pre = head;

            while (cur1 != null && cur2 != null) {
                if(cur1.val >= cur2.val){
                    pre.next = cur2;
                    cur2 = cur2.next;
                }else {
                    pre.next = cur1;
                    cur1 = cur1.next;
                }
                pre = pre.next;
            }

            pre.next = cur1 == null ? cur2 : cur1;
            return head;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        ;
    }
}
